package com.ten.mall.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ten.mall.vo.ImageResult;

@Controller
public class PageController {
	@RequestMapping("/admin")
	public String index(){
		return "jsp/index";
	}
	
	@RequestMapping("/admin/pic")
	@ResponseBody
	public String picUpload(@RequestParam(value="file") MultipartFile file,
							HttpServletRequest req){
		String path=req.getSession().getServletContext().getRealPath("/upload");
		String result="";
		String error="";
		String msg="";
			try {
				String fileName =""+new Date().getTime()+file.getOriginalFilename();
				File newFile=new File(path,fileName);
				file.transferTo(newFile);
				String imgurl="/upload/"+fileName;
				result ="{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + imgurl + "'}";
				//System.out.println("rs:"+result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result ="0";
			}
		return result;
	}
	/**
	 * 文件批量上传
	 * @param file
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/admin/picUpload",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String pictureUpload(@RequestParam(value="file") MultipartFile file,
								HttpServletRequest req){
		String path=req.getSession().getServletContext().getRealPath("/upload");
		String fileName ="";
		String rs="error";
		try {
			//String [] oldName=file.getOriginalFilename().split("\\.");
			//fileName =UUID.randomUUID().toString().replace("-", "")+"."+oldName[1];
			fileName=new Date().getTime()+file.getOriginalFilename();
			File newFile=new File(path,fileName);
			file.transferTo(newFile);
			rs="/upload/"+fileName;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	@RequestMapping("/admin/kindUpload")
	@ResponseBody
	public ImageResult kindUpload(@RequestParam(value="imgFile") MultipartFile imgFile,
							HttpServletRequest req){
		String path=req.getSession().getServletContext().getRealPath("/upload");
		ImageResult result=new ImageResult();
			try {
				String fileName =new Date().getTime()+imgFile.getOriginalFilename();
				File newFile=new File(path,fileName);
				imgFile.transferTo(newFile);
				String imgurl="/upload/"+fileName;
				result.setError(0);
				result.setUrl(imgurl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.setError(1);
				result.setMessage("上传文件失败!");
			}
		return result;
	}
	@RequestMapping("/admin/welcome")
	public String welcome(){
		return "jsp/welcome";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/admin/orderStateQuery")
	public String queryOrder(){
		return "jsp/queryOrder";
	}
	
	@RequestMapping("/admin/detail")
	public String getDetail(String str,HttpServletRequest res){
		res.setAttribute("str", str);
		return "jsp/picDetail";
	}
	
	@RequestMapping("/admin/rePass")
	public String editPass(){
		return "jsp/rePass";
	}
}
